﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace MII_FIF.Layouts.MII_FIF.FormApproval
{
    public partial class Task : LayoutsPageBase
    {
        /* Code Punya Panji
        SqlConnection conn;
        DataTable dt;
        SqlDataAdapter adapt;
        //string connStr = "Data Source=sp2013-server;Initial Catalog=InventoryManagement;User ID=sa;Password=P@ssw0rd";
        string queryStr = "";

        protected DataTable ExecuteDataTable(string queryStr)
        {
            //conn = new SqlConnection(connStr);
            conn = new SqlConnection(Configuration.DBConnection());
            conn.Open();
            adapt = new SqlDataAdapter(queryStr, conn);
            dt = new DataTable();
            adapt.Fill(dt);
            conn.Dispose();
            return dt;
        }
         * */

        protected void Page_Load(object sender, EventArgs e)
        {
            //((Label)Header.FindControl("lblHeader")).Text = "My Workflow Task";
            GridTask.RowCommand += GridTask_RowCommand;
            GridTask.PageIndexChanging += GridTask_PageIndexChanging;
            btnFilter.Click += btnFilter_Click;
            if (!IsPostBack)
            {
                load();
            }
        }

        void btnFilter_Click(object sender, EventArgs e)
        {
            load();
        }

        void GridTask_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            GridTask.PageIndex = e.NewPageIndex;
            GridTask.DataSource = ViewState["DT"];
            GridTask.DataBind();
        }

        void GridTask_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "data cabang")
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/ApprovalDataCabang.aspx?ID=" + e.CommandArgument));
            else if (e.CommandName.ToLower() == "data kunjungan")
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Approval_DataKunjungan.aspx?ID=" + e.CommandArgument));
            else if (e.CommandName.ToLower() == "change request network")
                Response.Redirect(Generic.GenLink("/_layouts/MII_FIF/FormApproval/Approval_CRNetwork.aspx?ID=" + e.CommandArgument));
            else if (e.CommandName.ToLower() == "change request physical")
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Approval_CRPhysical.aspx?ID=" + e.CommandArgument));
            else if (e.CommandName.ToLower() == "change request vm")
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Approval_CRVM.aspx?ID=" + e.CommandArgument));
            else if (e.CommandName.ToLower() == "change request storage")
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Approval_CRStorage.aspx?ID=" + e.CommandArgument));
        }

        void load()
        {
            if (Generic.isApprover())
            {
                SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InventoryManagement"].ConnectionString);
                con.Open();
                SqlCommand com = new SqlCommand("sp_Select_Task", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Status", DDLStatus.SelectedItem.Value);
                SqlDataAdapter sda = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                con.Close();

                foreach (DataRow row in dt.Rows)
                {
                    string userlogin = row["Requester"].ToString();
                    row["Requester"] = Generic.getUserName(userlogin);
                }
                dt.AcceptChanges();


                //Code Punya Panji
                //queryStr = @"EXEC sp_Select_Task";
                //dt = ExecuteDataTable(queryStr);
                ViewState["DT"] = dt;
                GridTask.DataSource = ViewState["DT"];
                GridTask.DataBind();
            }
            else
            {
                SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InventoryManagement"].ConnectionString);
                con.Open();
                SqlCommand com = new SqlCommand("sp_Select_Task", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Requester", Generic.GetUserLoginReformat());
                com.Parameters.AddWithValue("@Status",DDLStatus.SelectedItem.Value);
                SqlDataAdapter sda = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                con.Close();
                foreach (DataRow row in dt.Rows)
                {
                    string userlogin = row["Requester"].ToString();
                    row["Requester"] = Generic.getUserName(userlogin);
                }
                dt.AcceptChanges();
                ViewState["DT"] = dt;
                GridTask.DataSource = ViewState["DT"];
                GridTask.DataBind();
                //lblError.Text = "You are not approver. Please contact your system administrator.";
            }
        }
    }
}
